############
### FIGURE 1 CODE #####

rm(list = ls())
setwd() #Set working directory

# Packages
pacman::p_load(rtweet,
               ggplot2,
               purrrlyr,
               dplyr,
               readxl,
               writexl,
               broom,
               tidyverse,
               dotwhisker,
               lubridate,
               stargazer,
               ggpubr, 
               jtools,
               rpart,
               caret,
               reshape2,
               ipred,
               RColorBrewer,
               rpart.plot,
               MASS,
               randomForest,
               dplyr,
               car,
               vtable,
               effects,
               interactions
               
)

set.seed(0611)
#Import data
df <- readRDS("df.RDS")

#Subtract 1 b/c as.numeric converts factor var from 0 -> 1 and 1 -> 2
###Need to account for that by subtracting one
df$in.memoriam <- as.numeric(df$in.memoriam)-1
df$news.coverage <- as.numeric(df$news.coverage)-1
df$anti.conspir <- as.numeric(df$anti.conspir)-1

######################
######################
#Descriptive Analysis
######################
######################

#First figure out what the trends look like descriptively 
df.descrip <-dplyr::select(df, month2, conspir.gen, conspir.po, conspir.tusk, 
                           conspir.collusion, 
                           anti.conspir,  in.memoriam, news.coverage)

df.descrip2 <- df.descrip %>%                                   
  group_by(month2) 

df.descrip2 <- rename(df.descrip2, Month = month2)

df.descrip2$Month <- factor(df.descrip2$Month,
                            levels = c("Aug", "Sep", "Oct", "Nov", "Dec",
                                       "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"),
                            labels = c("Aug 21", "Sep 21", "Oct 21", "Nov 21", "Dec 21",
                                       "Jan 22", "Feb 22", "Mar 22", "Apr 22", "May 22",
                                       "Jun 22", "Jul 22"))

# Means

means <- summarise(df.descrip2,
                   "Total Conspiracy" = mean(conspir.gen, na.rm = T),
                   "PO Conspiracy" = mean(conspir.po, na.rm = T),
                   "Tusk Conspiracy" = mean(conspir.tusk, na.rm = T),
                   "Russia-PO Collaboration" = mean(conspir.collusion, na.rm = T),
                   "Anti-Conspiracy" = mean(anti.conspir, na.rm = T),
                   "In Memoriam" = mean(in.memoriam, na.rm = T),
                   "News Coverage" = mean(news.coverage, na.rm = T)
)

percents <- means*100

## visualizing
plot.descrip <-dplyr::select(means, Month, `PO Conspiracy`, `Tusk Conspiracy`, 
                             `Russia-PO Collaboration`, `In Memoriam`, `Total Conspiracy`,
                             `News Coverage`, `Anti-Conspiracy`)

month <- plot.descrip$Month
plot.descrip$month2 <- factor(month,
                              levels = c("Aug", "Sep", "Oct", "Nov", "Dec",
                                         "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"),
                              labels = c("Aug 21", "Sep 21", "Oct 21", "Nov 21", "Dec 21",
                                         "Jan 22", "Feb 22", "Mar 22", "Apr 22", "May 22",
                                         "Jun 22", "Jul 22"))

plot.descrip %>%
  pivot_longer(cols = 2:8) -> long

long$name<- factor(long$name,
                   levels = c("Total Conspiracy", "PO Conspiracy", "Tusk Conspiracy",
                              "Russia Conspiracy", "Russia-PO Collaboration", "In Memoriam",
                              "News Coverage", "Anti-Conspiracy"))

###### FIGURE 1 ########

long$CT <- ifelse(long$name %in% c("Total Conspiracy", "PO Conspiracy", "Tusk Conspiracy",
                                   "Russia Conspiracy", "Russia-PO Collaboration"), "CTs", "Non-CTs")

long %>%
  ggplot(aes(x = Month, y = value, fill = factor(name))) +
  geom_bar(stat = "identity", position = "dodge", colour="black") + #, name = "Tweet Type") +
  labs(y = "Proportion of Total Tweets",
       x = "Month",
       fill = "Tweet Type", 
       caption = "Dotted line for after Russia full-scale invasion of Ukraine on 24 February 2022") +
  facet_wrap(~ CT, ncol = 1) +
  scale_fill_manual(values = 
                      c("#a70000", "#ff0000", "#ff5252",
                        "#ffbaba", "#b3cde0",
                        "#6497b1", "#005b96")) +
  geom_vline(xintercept= 7.5, linetype = "dotted") +
  theme_minimal(base_size=15)  -> descriptive_graphic

#ggsave("Figure_1.pdf", descriptive_graphic, width = 11, height = 8.5)

